﻿package com.sz.ticket.servlet;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.sz.common.ConstDefine;
import com.sz.common.ICommonDAO;
import com.sz.login.bmo.ILoginBMO;
import com.sz.manager.dao.IManagerDAO;
import com.sz.ticket.bmo.ITicketBMO;

import org.springframework.web.servlet.mvc.Controller;

public class OperateTicket extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	protected final Log logger = LogFactory.getLog(getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			String type = request.getParameter("OPERATE_TYPE");
			String rtn_type = request.getParameter("RETURNTYPE");
			if(type.compareToIgnoreCase("LOCK") == 0) {
				lockTicket(request, response);				
			} else if(type.compareToIgnoreCase("EDIT") == 0) {
				editTicket(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("NEXTSTATUS") == 0) {
				nextStatus(request, response);
			} else if(type.compareToIgnoreCase("CANCEL") == 0) {
				
				cancelTicket(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("CONFIRMFAILED") == 0) {				
				confirmfailedTicket(request, response, rtn_type);
			} else if(type.compareToIgnoreCase("CHECK") == 0) {				
				checkTicket(request, response);
			} else if(type.compareToIgnoreCase("CONFIRM") == 0) {				
				confirmTicket(request, response, rtn_type);
			}
	}
	
	@SuppressWarnings("unchecked")
	private void lockTicket(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("TICKETS");
				List list = (List)JSONSerializer.toJSON(json);
				for(int n = 0; n < list.size(); ++n) {
					Map map = (Map)list.get(n);
					if(map.get("LOCKFLAG").toString().equals("1")) {
						//锁定操作 取当前用户
						map.put("LOCK_MANAGER", userid);
					}
				}

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				String msg = ticket.lockTickets(list);
				
				response.setCharacterEncoding("utf-8");
				response.getWriter().print(msg);		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void editTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域				
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);
				param.put("OPERATOR", userid);				
				
				String callback = request.getParameter("final_callback");
				if(callback == null) callback = "";
				
				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				Map r = ticket.editTicket(param);
				String msg = r.get("msg").toString();
				String msg_encode = URLEncoder.encode(r.get("msg").toString(), "UTF-8");
				String path = request.getContextPath();
				
				if(r.get("code").toString().equals("1")) {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {						
						response.sendRedirect(path + "/page/success.jsp?MESSAGE="+msg_encode+"&callback="+callback);
					}					
				} else {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {						
						response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg_encode);
					}
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void nextStatus(HttpServletRequest request, HttpServletResponse response) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);
				param.put("OPERATOR", userid);

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				String msg = ticket.nextStatus(param);
				
				String path = request.getContextPath(); 
				if(msg == null) {					
					response.sendRedirect(path + "/page/success.jsp");
				} else {
					msg = URLEncoder.encode(msg, "UTF-8");
					response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	@SuppressWarnings("unchecked")
	private void cancelTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);
				param.put("OPERATOR", userid);

				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				String msg = ticket.cancelTicket(param);
				
				String path = request.getContextPath(); 
				if(msg == null) {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print("撤单成功");
					} else {
						response.sendRedirect(path + "/page/success.jsp");
					}					
				} else {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {
						msg = URLEncoder.encode(msg, "UTF-8");
						response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
					}
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void confirmfailedTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);
				param.put("OPERATOR", userid);

				String callback = request.getParameter("final_callback");
				if(callback == null) callback = "";
				
				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				String msg = ticket.confirmfailedTicket(param);
				
				String path = request.getContextPath(); 
				if(msg == null) {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print("操作成功");
					} else {
						response.sendRedirect(path + "/page/success.jsp"+"?callback="+callback);
					}					
				} else {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {
						msg = URLEncoder.encode(msg, "UTF-8");
						response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg);
					}
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
	
	@SuppressWarnings("unchecked")
	private void checkTicket(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {

			String path = request.getContextPath();

			String userid = request.getSession().getAttribute("userid").toString();

			String callback = request.getParameter("final_callback");
			if(callback == null) callback = "";
			
			ServletContext servletContext = request.getSession().getServletContext();
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);

			String msg = "";

			String ticket_id = request.getParameter("ticket_id");
			if (ticket_id == null || ticket_id.equals("")) {
				msg = URLEncoder.encode("参数错误", "UTF-8");
				response.sendRedirect(path + "/page/failure.jsp?MESSAGE=" + msg);
			}

			String from = request.getParameter("from");
			if (from == null || (from.equals("1") == false && from.equals("2") == false)) {
				msg = URLEncoder.encode("参数错误", "UTF-8");
				response.sendRedirect(path + "/page/failure.jsp?MESSAGE=" + msg);
			}

			ITicketBMO bmo = (ITicketBMO) context.getBean("TicketBMO");

			if (from.equals(ConstDefine.s_fromBusiness)) {
				String check_b_flag = request.getParameter("check_b_flag");
				String check_b_time = request.getParameter("check_b_time");
				String check_b_consume_normal = request.getParameter("check_b_consume_normal");
				String check_b_consume_offcard = request.getParameter("check_b_consume_offcard");
				String check_b_consume_savecard = request.getParameter("check_b_consume_savecard");
				String check_b_consume_other = request.getParameter("check_b_consume_other");
				String check_b_staffnum = request.getParameter("check_b_staffnum");
				String check_b_env = request.getParameter("check_b_env");
				String check_b_position = request.getParameter("check_b_position");
				String check_b_contact = request.getParameter("check_b_contact");
				String check_b_comment = request.getParameter("check_b_comment");
				String check_b_number = request.getParameter("check_b_number");
				String check_b_total = request.getParameter("check_b_total");
				String check_b_manager = userid;
				String business_response = request.getParameter("business_response");

				Map map = new HashMap();
				map.put("CHECK_B_FLAG", check_b_flag);
				map.put("CHECK_B_TIME", check_b_time);
				map.put("CHECK_B_CONSUME_NORMAL", check_b_consume_normal);
				map.put("CHECK_B_CONSUME_OFFCARD", check_b_consume_offcard);
				map.put("CHECK_B_CONSUME_SAVECARD", check_b_consume_savecard);
				map.put("CHECK_B_CONSUME_OTHER", check_b_consume_other);
				map.put("CHECK_B_STAFFNUM", check_b_staffnum);
				map.put("CHECK_B_ENV", check_b_env);
				map.put("CHECK_B_POSITION", check_b_position);
				map.put("CHECK_B_CONTACT", check_b_contact);
				map.put("CHECK_B_COMMENT", check_b_comment);
				map.put("CHECK_B_NUMBER", check_b_number);
				map.put("CHECK_B_TOTAL", check_b_total);
				map.put("CHECK_B_MANAGER", check_b_manager);
				map.put("TICKET_ID", ticket_id);
				map.put("BUSINESS_RESPONSE", business_response);

				bmo.checkTicket_Business(map);
				
				response.sendRedirect(path + "/page/success.jsp"+"?callback="+callback);
			} else if (from.equals(ConstDefine.s_fromCustom)) {
				
				String check_c_flag = request.getParameter("check_c_flag");
				String check_c_time = request.getParameter("check_c_time");				
				String check_c_staffnum = request.getParameter("check_c_staffnum");				
				String check_c_comment = request.getParameter("check_c_comment");
				String check_c_number = request.getParameter("check_c_number");
				String check_c_total = request.getParameter("check_c_total");
				String check_c_remark = request.getParameter("check_c_remark");
				String check_c_manager = userid;
				String business_response = request.getParameter("business_response");
				
				Map map = new HashMap();
				map.put("CHECK_C_FLAG", check_c_flag);
				map.put("CHECK_C_TIME", check_c_time);
				map.put("CHECK_C_STAFFNUM", check_c_staffnum);
				map.put("CHECK_C_COMMENT", check_c_comment);
				map.put("CHECK_C_NUMBER", check_c_number);
				map.put("CHECK_C_TOTAL", check_c_total);
				map.put("CHECK_C_REMARK", check_c_remark);				
				map.put("CHECK_C_MANAGER", check_c_manager);
				map.put("TICKET_ID", ticket_id);
				map.put("BUSINESS_RESPONSE", business_response);

				bmo.checkTicket_Custom(map);
				
				response.sendRedirect(path + "/page/success.jsp"+"?callback="+callback);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new ServletException(e.getMessage());
		}

	}
	
	@SuppressWarnings("unchecked")
	private void confirmTicket(HttpServletRequest request, HttpServletResponse response, String rtn_type) 
				throws ServletException, IOException{
		try {				
				String userid = request.getSession().getAttribute("userid").toString(); //当前登陆用户的区域				
				
				String json = request.getParameter("PARAM");
				Map param = (Map)JSONSerializer.toJSON(json);
				param.put("OPERATOR", userid);				
				
				String callback = request.getParameter("final_callback");
				if(callback == null) callback = "";
				
				ServletContext servletContext = request.getSession().getServletContext();        
				ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
				
				ITicketBMO ticket = (ITicketBMO) context.getBean("TicketBMO");
				Map r = ticket.confirmTicket(param);
				String msg = r.get("msg").toString();
				String msg_encode = URLEncoder.encode(r.get("msg").toString(), "UTF-8");
				String path = request.getContextPath();
				
				if(r.get("code").toString().equals("1")) {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {						
						response.sendRedirect(path + "/page/success.jsp?MESSAGE="+msg_encode+"&callback="+callback);
					}					
				} else {
					if(rtn_type != null && rtn_type.compareToIgnoreCase("MESSAGE")==0) { //ajax方式调用
						response.setCharacterEncoding("utf-8");
						response.getWriter().print(msg);
					} else {						
						response.sendRedirect(path + "/page/failure.jsp?MESSAGE="+msg_encode);
					}
				}
		
			} catch (Exception e) {
				logger.error(e.getMessage());
				throw new ServletException(e.getMessage());		
			}
	}
}
